<?php
/**
 * Reconciliation
 *
 * PHP version 5
 *
 * @category Model
 * @package  Croogo
 * @version  1.0
 * @author   Tran Van Dung <dungtv@ic-t.info>
 * @license  http://www.opensource.org/licenses/mit-license.php The MIT License
 * @link     http://www.croogo.org
 */
class Reconciliation extends AppModel {
/**
 * Model name
 *
 * @var string
 * @access public
 */
    public $name = 'Reconciliation';
/**
 * Behaviors used by the Model
 *
 * @var array
 * @access public
 */
    public $actsAs = array(
        'Encoder',
        'Cached' => array(
            'prefix' => array(
                'reconciliation_',
            ),
        ),
    );
/**
 * Validation
 *
 * @var array
 * @access public
 */
    public $validate = array(
        'name_of_bank' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'address_of_bank' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'bank_code' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'branch_code' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'swift_code' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'amount' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'reference_of_transaction' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
        'date' => array(
            'rule' => 'notEmpty',
            'message' => 'This field cannot be left blank.',
        ),
    );

    public function bmatch($data=array()) {
        if($data) {
            if(isset($data['id'])) {
                // update
                $sql = 'UPDATE `cc_bmatches` SET ';
                $set = '';
                foreach ($data as $k => $v) {
                    $set .= $set=='' ? "`".$k."`='".$v."'" : ", `".$k."`='".$v."'";
                }
                $sql .= $set . " WHERE `id`=".$data['id'];
                return $this->query($sql);
            } else {
                // insert
                $sql = 'INSERT INTO `cc_bmatches` ';
                $fields = '';
                $values = '';
                foreach ($data as $k => $v) {
                    $fields .= $fields=='' ? "`".$k."`" : ", `".$k."`";
                    $values .= $values=='' ? "'".$v."'" : ", '".$v."'";
                }
                $sql .= "(" . $fields . ") VALUES (" . $values . ")";
                return $this->query($sql);
            }
        }
        return false;
    }
    
    public function del_bmatch($id=0) {
        return $this->query("DELETE FROM `cc_bmatches` WHERE `id`=".$id);
    }
    
}
?>